<?php
class HotcityAction extends AdminPublicAction{
	/** =============
	 列表
	 =================**/
	public function index()
	{
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = "c.".I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "c.id";
			$_GET['updown'] = $upDown = "desc";
				
		}
		//echo $sort.$upDown;die;
		$keyword = I('keyword','');
	
		if(tep_not_null($keyword))$where['c.name']  = array('like','%'.$keyword.'%');
		$where['c.hot_flag']=1;
		import("ORG.Util.Page");
		$page = 10;
		$count = D('CityView')->where($where)->count();
		$Page = new Page($count, $page);
		$list   = D('CityView')->getAll($where,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		$show = $Page->show();
	//var_dump($list);die;
			
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
		$this->keyword = $keyword;
		$this->listing = $list;
		$this->page = $show;
		$this->left_hotcity = 'current';
		$this->display();
	
	}
	/** =============
	 添加 & 編輯
	 =================**/
	public function  detail(){
		$this->action = 'addHandle';
		$this->actionName = 'Add';
		//获取所有省份和城市
		$this->allProvince   = M('province')->select();
		$this->allCity   = M('city')->select();
		$this->e_allProvince   = json_encode($this->allProvince);
		$this->e_allCity   = json_encode($this->allCity);
		
		
		$this->left_hotcity = 'current';
		$this->display();
	
	}
	/** =============
	 添加处理
	 =================**/
	
	public function addHandle()
	{
		if(IS_POST){
			$citycode = I('citycode');
			$data['code'] = $citycode;
			M('city')->where(array('code'=>$citycode))->setField('hot_flag', 1);
				
			S('success','热门城市添加成功!');
			$this->redirect('index');
		}else{
			$this->redirect('index');
		}
			
	}

	
	public function delete()
	{
		$id  = I('id');
		if(tep_not_null($id) && is_numeric($id)){
			M('city')->where(array('id' => $id))->setField('hot_flag', 0);
			S('success','删除热门城市成功!');
			$this->redirect('index');
		}else{
			S('err','删除热门城市失败!');
			$this->redirect('index');
		}
	}
	
	public function changeStatus(){
		$check = M("city")->where(array("id" => (int)$_GET['id']))->getField('hot_flag');
		$news_status = ($check == 1)?0:1;
		M("city")->where(array("id" => (int)$_GET['id']))->setField('hot_flag',$news_status);
		echo (int)$news_status;
			
	}
	
	
}